<?php
session_start();
if(!$_SESSION['user']){
    header('location: login.php');//若没有登录，直接重定向到登录页面
    return;
}
$u = $_SESSION['user']; //当前登录的用户信息
?><!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>通讯录</title>
    <style>
        h1{ color:red;}
        table{
            border: 5px double red;
            width: 500px;
        }
        th{ color:red; }
        th, td{
            border-bottom: 1px dotted red;
            border-right: 1px dotted red;
            padding: 8px;
            word-break: keep-all;
        }
        .nav{
            display: flex;
            width: 500px;
            text-align: right;
            padding: 5px 0;
        }
        a{
            color: gray;
            text-decoration: none;
        }
        a:hover{
            color:red;
            text-decoration: underline;
        }
        td:last-child{
            text-align: center;
        }
        .user{
            color:red;
            text-align: left;
            font-weight: bold;
            flex-grow: 1;
        }
    </style>

</head>
<body>
<h1>我的通讯录</h1>
<div class="nav">
    <div>欢迎你：</div>
    <div class="user"><?=htmlentities($u['xh']), htmlentities($u['name'])?></div>
    <a href="logout.php">注销</a>
</div>
<?php
$db=new PDO('mysql:host=localhost;dbname=db','root','12qwas');
$ps=$db->query("select * from students order by xh");
echo "<table>";
echo "<tr><th>学号</th><th>姓名</th><th>手机</th><th>管理</th></tr>";
while($r=$ps->fetch(PDO::FETCH_ASSOC)){
    echo "<tr><td>".htmlentities($r['xh'])."</td><td>".htmlentities($r['name'])."</td><td>".htmlentities($r['tel'])."</td><td><a href='delete.php?xh=".urlencode($r['xh'])."'>删除</a> <a href='modify.php?xh=".urlencode($r['xh'])."'>修改</a></td></tr>";
    echo "<tr><td>".htmlentities($r['xh'])."</td><td>".htmlentities($r['name'])."</td><td>".htmlentities($r['tel'])."</td><td>";
    if($r['xh']===$u['xh'])echo "<a href='delete.php?xh=".urlencode($r['xh'])."'>删除</a> <a href='modify.php?xh=".urlencode($r['xh'])."'>修改</a>";
    echo "</td></tr>";
}
echo "</table>";
?>
</body>
</html>